//
//  ZWView.m
//  MyTest
//
//  Created by rayootech on 15/9/26.
//  Copyright (c) 2015年 rayootech. All rights reserved.
//

#import "ZWView.h"
@interface ZWView()
@property (strong, nonatomic) NSMutableArray *paths;
@end
@implementation ZWView

- (NSMutableArray *)paths
{
    if (_paths == nil) {
        _paths = [NSMutableArray array];
    }
    return _paths;
}

- (void)back
{
    [self.paths removeLastObject];
    [self setNeedsDisplay];
}

- (void)clear
{
    [self.paths removeAllObjects];
    [self setNeedsDisplay];
}

- (void)drawRect:(CGRect)rect
{
    for (UIBezierPath *path in self.paths) {
        [path stroke];
    }
}

/**
 确定起点
 */
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //创建一个新的路径，一个贝塞尔路径就代表一条完整的路径
    UIBezierPath *path=[UIBezierPath bezierPath];
    path.lineCapStyle=kCGLineCapRound;
    path.lineJoinStyle=kCGLineCapRound;
    path.lineWidth=5;
    
    //获得当前的触摸点
    UITouch *touch=[touches anyObject];
    [path moveToPoint:[touch locationInView:self]];
    [self.paths addObject:path];
    [self setNeedsDisplay];
}

/**
 连线
 */
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIBezierPath *path=[self.paths lastObject];
    UITouch *touch=[touches anyObject];
    [path addLineToPoint:[touch locationInView:self]];
    [self setNeedsDisplay];
}

/**
 连线
 */
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self touchesMoved:touches withEvent:event];
}


@end
